package com.powersystem.demo.controller;


import com.powersystem.demo.entity.PsFunction;
import com.powersystem.demo.service.IPsFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.swing.text.PasswordView;
import javax.swing.text.html.parser.Entity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author linrongji
 * @since 2019-11-14
 */
@RestController
@RequestMapping("/psFunction")
public class PsFunctionController {
    @Autowired
    private IPsFunctionService IPsFunctionService;

    @GetMapping(value = "index")
    public String index() {
        List<PsFunction> listPS = IPsFunctionService.list();
        Map<Integer, List<PsFunction>> listMap = new HashMap<>();
        for (PsFunction each : listPS) {
            if (listMap.get(each.getParentid()) == null) {
                List<PsFunction> listps = new ArrayList<>();
                listps.add(each);
                listMap.put(each.getParentid(), listps);
            } else {
                listMap.get(each.getParentid()).add(each);
            }

        }

        System.out.println(html(0,listMap));
        return "";
    }

    public static String html(int parentId, Map<Integer, List<PsFunction>> map) {

        StringBuffer sp = new StringBuffer();
        if(map.get(parentId) ==null){
            return "";
        }
        for (PsFunction psFun : map.get(parentId)) {

            if(parentId == 0){
                sp.append("<view class='parentId'>");

            }else{
                sp.append("<view >");
            }
            sp.append(psFun.getFunctionname() +html(psFun.getId().intValue(), map)+ "</view>");
        }
        return sp.toString();
    }


}

